package com.pachong.android.frameworkbase.imageloader.uil;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.pachong.android.frameworkbase.imageloader.IImageLoader;
import com.pachong.android.frameworkbase.imageloader.ImageLoaderCallback;

/**
 * Universal Image Loader 对图片加载接口的实现类
 *
 * 注意：需要在Application中初始化
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016/6/23 16:59
 * @Version: 1.0.0
 */
public class UIL implements IImageLoader {

    private Context mContext;
    private static DisplayImageOptions mDefOption;
    private Uri mUri;
    private ImageView mImageView;
    private ImageLoaderCallback mCallback;

    public UIL(Context context) {
        mContext = context;
    }


    @Override
    public IImageLoader load(Uri uri) {
        mUri = uri;
        return this;
    }



    /**
     * @param iv
     */
    @Override
    public IImageLoader into(ImageView iv) {
        mImageView = iv;
        return this;
    }

    @Override
    public IImageLoader callback(ImageLoaderCallback cb) {
        mCallback = cb;
        return this;
    }

    @Override
    public IImageLoader skipMemory(boolean bl) {
        Log.w("UIL", "skipMemory");
        return null;
    }

    @Override
    public IImageLoader asGif(boolean bl) {
        return null;
    }

    @Override
    public IImageLoader resize(boolean bl, int width, int height) {
        return null;
    }

    @Override
    public void clear() {
        Log.w("UIL", "clear");
    }

    /**
     *  TODO: Lots of things to do  URI不为http 就会有bug，后期要扩展
     */
    @Override
    public void start() {
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(mUri.toString(), mImageView);
    }

    public static void init(Context context){
        initImageLoader(context);
        mDefOption = UILImageOptionFactory.createImageOption(UILImageOptionFactory.OPTION_DEF);

    }

    public static void initImageLoader(Context context) {
        ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .diskCacheSize(20 * 1024 * 1024) // 20 Mb
                .tasksProcessingOrder(QueueProcessingType.LIFO);
//		if (Loger.LOG_DEBUG) {
//			builder.writeDebugLogs();
//		}
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(builder.build());
    }

}
